package com.wechat.servlet;

import com.wechat.DAO.DaoImpl.UserDAOIpml;
import com.wechat.DAO.UserDAO;
import com.wechat.pojo.User;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  IOException {
        UserDAO userDAO = new UserDAOIpml();
        int uid = Integer.parseInt(request.getParameter("uid"));
        String nickname = request.getParameter("nickname");


        User user = userDAO.query(uid);
        if(user==null){
            if(nickname != null){
                //执行注册
                userDAO.add(new User(uid,nickname,0,0,0));
                response.getWriter().write("success");
                System.out.println("注册成功:"+uid);
            }else {
                response.getWriter().write("fail");
                System.out.println("非法输入，注册失败");
            }
        }else {
            //用户已存在代码
            response.getWriter().write("fail");
        }
    }
}
